package com.ldoublem.loadingviewlib.view;

import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.render.Arc;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.render.Path;
import ohos.agp.utils.Color;
import ohos.agp.utils.RectFloat;
import ohos.app.Context;

import com.ldoublem.loadingviewlib.view.base.LVBase;


public class LVBattery extends LVBase implements Component.EstimateSizeListener, Component.DrawTask{
    private float mWidth = 0f;
    private float mhigh = 0f;

    private float mBatteryWidth;
    private float mBatteryHigh;
    private float mPadding = 0f;
    private float mBodyCorner = 0f;

    private float mBatterySpace = 0f;

    private Paint mPaint, mPaintHead, mPaintValue;


    private BatteryOrientation mBatteryOrientation = BatteryOrientation.HORIZONTAL;


    RectFloat rectFBody = null;
    RectFloat rectHead = null;

    public LVBattery(Context context) {
        super(context);
    }

    public LVBattery(Context context, AttrSet attrs) {
        super(context, attrs);
    }

    public LVBattery(Context context, AttrSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void onDraw(Component component, Canvas canvas) {
        if (mBatteryOrientation == BatteryOrientation.VERTICAL)
            canvas.rotate(270, mWidth / 2, mWidth / 2);
        else
            canvas.rotate(0, mWidth / 2, mWidth / 2);
        canvas.save();

        drawHead(canvas);
        drawBody(canvas);
        drawValue(canvas);
        drawLogo(canvas);
        canvas.restore();
    }

    @Override
    public boolean onEstimateSize(int i, int i1) {
        if (EstimateSpec.getSize(i) > getHeight()) {
            mWidth = EstimateSpec.getSize(i1);
            mhigh = EstimateSpec.getSize(i1) * 0.8f;

        } else {
            mWidth = EstimateSpec.getSize(i);
            mhigh = EstimateSpec.getSize(i) * 0.8f;

        }
        return false;
    }

    public enum BatteryOrientation {
        VERTICAL, HORIZONTAL
    }

    private boolean mShowNum = false;




    private void drawHead(Canvas canvas) {
        float mHeadwidth = mhigh / 6;
        rectHead = new RectFloat(
                mWidth - mPadding - mHeadwidth,
                mWidth / 2 - mHeadwidth / 2,
                mWidth - mPadding,
                mWidth / 2 + mHeadwidth / 2);
        canvas.drawArc(rectHead, new Arc( -70, 140
                , false), mPaintHead);
    }


    private void drawBody(Canvas canvas) {
        float mHeadwidth = mhigh / 6;
        float x = (float) ((mHeadwidth / 2) * Math.cos(-70 * Math.PI / 180f));
        rectFBody = new RectFloat();
        rectFBody.top = mWidth / 2 - mhigh / 4 + mPadding;
        rectFBody.bottom = mWidth / 2 + mhigh / 4 - mPadding;
        rectFBody.left = mPadding;
        rectFBody.right = mWidth - mPadding - x - x - mBatterySpace;
        canvas.drawRoundRect(rectFBody, mBodyCorner, mBodyCorner, mPaint);
    }

    private void drawValue(Canvas canvas) {
        RectFloat rectFBatteryValueFill = new RectFloat();
        rectFBatteryValueFill.top = rectFBody.top + mBatterySpace;
        rectFBatteryValueFill.bottom = rectFBody.bottom - mBatterySpace;
        rectFBatteryValueFill.left = mPadding + mBatterySpace;
        rectFBatteryValueFill.right = rectFBody.right - mBatterySpace;

        RectFloat rectFBatteryValue = new RectFloat();
        rectFBatteryValue.top = rectFBatteryValueFill.top;
        rectFBatteryValue.bottom = rectFBatteryValueFill.bottom;
        rectFBatteryValue.left = rectFBatteryValueFill.left;
        rectFBatteryValue.right = rectFBatteryValueFill.right * mAnimatedValue;
        canvas.drawRoundRect(rectFBatteryValue, 1, 1, mPaintValue);
    }

    private void drawLogo(Canvas canvas) {

        mPaintHead.setTextSize((int) (mhigh / 6));
        if (!mShowNum) {
            Path path = new Path();
            path.moveTo(mWidth / 2 - mhigh / 6, mWidth / 2 - dip2px(1.5f));
            path.lineTo(mWidth / 2 + dip2px(2f), mWidth / 2 + mhigh / 12);
            path.lineTo(mWidth / 2 + dip2px(1f), mWidth / 2);
            path.close();
            canvas.drawPath(path, mPaintHead);
            Path path2 = new Path();
            path2.moveTo(mWidth / 2 - dip2px(2f), mWidth / 2 - mhigh / 12);
            path2.lineTo(mWidth / 2 + mhigh / 6, mWidth / 2 + dip2px(1.5f));
            path2.lineTo(mWidth / 2 - dip2px(1f), mWidth / 2);
            path2.close();
            canvas.drawPath(path2, mPaintHead);
        } else {
            String text = (int) (mAnimatedValue * 100) + "%";

            if (mBatteryOrientation == BatteryOrientation.VERTICAL) {
                Path p = new Path();
                p.moveTo(mWidth / 2, 0);
                p.lineTo(mWidth / 2, mWidth);
                canvas.drawTextOnPath(mPaintHead,text, p,
                        mWidth / 2 - getFontlength(mPaintHead, text) / 2,
                        mWidth / 2 - mhigh / 2 - getFontHeight(mPaintHead, text) / 2
                        );
            } else {
                canvas.drawText(mPaintHead,text,
                        mWidth / 2 - getFontlength(mPaintHead, text) / 2,
                        mWidth / 2 + getFontHeight(mPaintHead, text) / 2
                        );
            }

        }
    }




    private void initPaint() {
        setEstimateSizeListener(this);
        addDrawTask(this);
        mBatteryHigh = dip2px(20);
        mPadding = dip2px(2);
        mBodyCorner = dip2px(1);
        mBatterySpace = dip2px(1);
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.STROKE_STYLE);
        mPaint.setColor(Color.WHITE);
        mPaintHead = new Paint();
        mPaintHead.setAntiAlias(true);
        mPaintHead.setStyle(Paint.Style.FILL_STYLE);
        mPaintHead.setColor(Color.WHITE);
        mPaintValue = new Paint();
        mPaintValue.setAntiAlias(true);
        mPaintValue.setStyle(Paint.Style.FILL_STYLE);
        mPaintValue.setColor(new Color(Color.rgb(67, 213, 81)));
    }



    public void setViewColor(int color)
    {
        mPaint.setColor(new Color(color));
        mPaintHead.setColor(new Color(color));
        invalidate();
    }

    public void setCellColor(int color)
    {
        mPaintValue.setColor(new Color(color));
        invalidate();
    }




    public void setValue(int value)//0-100
    {
        this.mAnimatedValue = value * 1.f / 100;
        invalidate();
    }

    public void setShowNum(boolean show) {
        this.mShowNum = show;
        invalidate();
    }

    public void setBatteryOrientation(BatteryOrientation batteryOrientation) {
        this.mBatteryOrientation = batteryOrientation;
        invalidate();
    }


    private float mAnimatedValue = 0f;


    @Override
    protected void InitPaint() {
        initPaint();
    }

    @Override
    protected void OnAnimationUpdate(AnimatorValue valueAnimator, float v) {
        mAnimatedValue = v;
        invalidate();
    }

    @Override
    protected void OnAnimationRepeat(Animator animation) {

    }

    @Override
    protected int SetAnimRepeatCount() {
        return AnimatorValue.INFINITE;
    }

    @Override
    protected int OnStopAnim() {
        mAnimatedValue = 0f;
        invalidate();
        return 1;
    }

    @Override
    protected void AinmIsRunning() {

    }

    @Override
    protected int SetAnimRepeatMode() {
        return RESTART;
    }


}
